package com.iplant.wms.serviceImpl.dao.mrp;

import com.iplant.base.po.OutResult;
import com.iplant.base.po.Pagination;
import com.iplant.base.po.enums.ExceptionTypes;
import com.iplant.base.utils.annotation.dao.DBBaseDAO;
import com.iplant.base.utils.general.StringUtils;
import com.iplant.base.utils.sql.BaseDAO;
import com.iplant.basic.service.po.bms.BMSEmployee;
import com.iplant.wms.service.po.mrp.MRPDeliveryItem;
import lombok.extern.slf4j.Slf4j;

import java.util.*;


@DBBaseDAO(MRPDeliveryItem.class)
@Slf4j
public class MRPDeliveryItemDAO extends BaseDAO {

    private static MRPDeliveryItemDAO Instance = null;

    public static MRPDeliveryItemDAO getInstance() {
        if (Instance == null) {
            Instance = new MRPDeliveryItemDAO();
        }
        return Instance;
    }

    public MRPDeliveryItemDAO() {
        super("");this.Init();
    }


    public void MRP_DeleteDeliveryItemByPlanIDList(BMSEmployee wLoginUser, List<Integer> wPlanIDList, OutResult<Integer> wErrorCode){

        if(wPlanIDList==null||wPlanIDList.size()==0)
            return;

        Map<String,Object> wData=new HashMap<>();

        wData.put("Active",2);
        wData.put("PlanFQTY",0);
        wData.put("OrderPlan",0);

        this.Update(wData,null, StringUtils.Format(" ID>0 AND DeliveryID IN ({0})",StringUtils.Join(",",wPlanIDList)));

        wData.put("FQTYDone",0);

        this.Delete(wData, StringUtils.Format(" DeliveryID IN ({0})",StringUtils.Join(",",wPlanIDList)));
    }

    public void MRP_DeleteDeliveryItemByIDList(BMSEmployee wLoginUser, List<Integer> wIDList, OutResult<Integer> wErrorCode){
        if(wIDList==null||wIDList.size()==0)
            return;

        Map<String,Object> wData=new HashMap<>();

        wData.put("Active",2);
        wData.put("PlanFQTY",0);
        wData.put("OrderPlan",0);

        this.Update(wData,null, StringUtils.Format(" AND ID IN ({0})",StringUtils.Join(",",wIDList)));

        wData.put("FQTYDone",0);

        this.Delete(wData, StringUtils.Format(" AND ID IN ({0})",StringUtils.Join(",",wIDList)));
    }


    public List<MRPDeliveryItem> MRP_SelectDeliveryItemByPlanIDList(BMSEmployee wLoginUser, List<Integer> wPlanIDList, Pagination wPagination, OutResult<Integer> wErrorCode) {

        if(wPlanIDList==null||wPlanIDList.size()==0)
            return new ArrayList<>();


        Map<String,Object> wParams=new HashMap<>();

        wParams.put("PlanID", wPlanIDList);

        return this.SelectPOList(wLoginUser, wParams, wPagination, wErrorCode);
    }





    public void WMS_UpdateDeliveryItemItemFQTY(BMSEmployee wLoginUser, Calendar wStartDate, Calendar wEndDate, OutResult<Integer> wErrorCode) {
        wErrorCode.set(0);
        try {
            //问题：同工位 不同工序 同物料  等同
            String wSQLText = StringUtils.Format("update {0} t set t.FQTYDone= " +
                            "( select ifnull( sum(t2.FQTY),0) from {1} t2 inner join {2} t3" +
                            " on  t2.StoreStationID=t3.StoreStationID " +
                            " and (t3.MaterialID<=0 or t3.MaterialID=t2.MaterialID)" +
                            " WHERE t2.StoreStationID>0 and find_in_set(t3.StationID,t.StationID) " +
                            " and t2.StockOperateRecordID>0 and t2.ReceiveBillItemID>0 " +
                            " and t2.MaterialID=t.MaterialID and t.OrderNo=t2.ProjectNo" +
                            " and DATE_FORMAT(t2.CreateTime, ''%Y-%m-01'') = DATE_FORMAT(t.DemandDate, ''%Y-%m-01'') )" +
                            " where  t.ID>0 and t.DemandDate>= :wStartDate and t.DemandDate< :wEndDate ",
                    this.getTableName(), this.getTableName("wms_outboundorderitem"),
                    this.getTableName("wms_resource"));

            Map<String,Object> wParams=new HashMap<>();
            wParams.put("wStartDate", wStartDate);
            wParams.put("wEndDate", wEndDate);
            this.Update(wSQLText,wParams);

        } catch (Exception e) {
            wErrorCode.Result = ExceptionTypes.DBSQL.getValue();
            log.error("WMS_UpdateDeliveryItemItemFQTY", e);
        }
    }

}
